package com.example.demo.bizhan.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description: xxx
 * @time: 2020-10-28 21:14
 * @author: baojinlong
 * 线程池作用:
 * Server一个服务:一个请求过来你的Server,你的Server就会开始一个线程来处理
 * 1) 创建线程
 * 2) 在线程中执行代码
 * 3) 销毁线程
 * ==>线程池:在应用启动的时候给你准备好了一些线程,你需要使用的时候直接拿来用,并不需要去创建.
 **/
public class ThreadPoolDemo {
    public static void main(String[] args) {
        System.out.println(Executors.newSingleThreadExecutor());
        System.out.println(Executors.newFixedThreadPool(2));
        System.out.println(Executors.newCachedThreadPool());
        System.out.println(Executors.newScheduledThreadPool(3));

        Thread t1 = new PkThread();
        Thread t2 = new PkThread();
        Thread t3 = new PkThread();
        Thread t4 = new PkThread();
        Thread t5 = new PkThread();


        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(t1);
        executorService.execute(t2);
        executorService.execute(t3);
        executorService.execute(t4);
        executorService.execute(t5);

        // 关闭线程池
        executorService.shutdown();
    }
}

